<?php
abstract class Controller {
	protected $id;
	protected $layout;
	protected $model;
	protected $template;
	protected $attributes = array();
	protected $children = array();
	protected $data = array();
	protected $output;

	public function __get($key) {
		$key = str_replace("_", "/", $key);
		return Singleton::getSingleton($key);
	}

	public function __set($key, $value) {
		$key = str_replace("_", "/", $key);
		Singleton::register($key, $value);
	}

	protected function forward($route, $args = array()) {
		return new Action($route, $args);
	}

	protected function redirect($url, $status = 302) {
		header('Status: ' . $status);
		header('Location: ' . str_replace(array('&amp;', "\n", "\r"), array('&', '', ''), $url));
		exit();				
	}
	
	private function loadPage(){
		$action = $this->engine_action->getAction();
		$pageConfig = $this->engine_loader->_get($action);
		$this->template = $pageConfig['template'];
		$this->attributes  = $pageConfig['attributes'];
	}

	protected function render() {$this->loadPage();

		if (file_exists(DIR_CONTENT . $this->template)) {
			extract($this->attributes);

			ob_start();

			require(DIR_CONTENT . $this->template);
			$this->response->setResult(ob_get_contents()) ;
			ob_end_clean();

			
 		} else {
			trigger_error('Error: Could not load template ' . DIR_CONTENT . $this->template . '!');
			exit();				
		} 
	}

}
?>